#include "PopupWidget.h"
#include "WebPopupMenu.h"
#include "WebViewProxy.h"
#include "WebSize.h"
#include "WebRect.h"
#include "WebScreenInfo.h"
#include "skia/ext/platform_canvas.h"

inline WebKit::WebCanvas* SkiaCanvasToWebConvas(skia::PlatformCanvas* canvas)
{
#ifndef WEBKIT_USING_SKIA
	return canvas->getTopPlatformDevice().GetBitmapContext();
#else
	return canvas;
#endif
}

PopupWidget::PopupWidget(WebViewProxy* parent) : parent(parent), canvas(0)
{
	widget = WebKit::WebPopupMenu::create(this);
}

PopupWidget::~PopupWidget()
{
	if (canvas)
		delete canvas;

	widget->close();
}

WebWidget* PopupWidget::getWidget()
{
	return widget;
}

void PopupWidget::renderToWebView(Awesomium::RenderBuffer* context, bool isParentTransparent)
{
	if (mWindowRect.IsEmpty())
		return;

	if (!canvas)
		canvas = new skia::PlatformCanvas(mWindowRect.width(), mWindowRect.height(), false);

	if (!dirtyArea.IsEmpty())
	{
		widget->layout();
		widget->paint(SkiaCanvasToWebCanvas(canvas), WebKit::WebRect(dirtyArea));
		dirtyArea = gfx::Rect();
	}

	const SkBitmap& sourceBitmap = canvas->getTopPlatformDeivice().accessBitmap(false);
	SkAutoLockPixels sourceBitmapLock(sourceBitmap);

	context->copyArea((unsigned char*)sourceBitmap.getPixels(), sourceBitmap.rowBytes(), mWindowRect, isParentTransparent);
}

void PopupWidget::didScrollWebView(int dx, int dy)
{
	if (mWindowRect.IsEmpty())
		return;

	mWindowRect.Offset(dx, dy);
}

bool PopupWidget::isVisible()
{
	return !mWindowRect.IsEmpty();
}

void PopupWidget::translatePointRelative(int& x, int& y)
{
	if (!mWindowRect.IsEmpty())
	{
		x -= mWindowRect.x();
		y -= mWindowrect.y();
	}
}

void PopupWidget::didInvalidateRect(const WebKit::WebRect& rect)
{
	gfx::Rect clientRect(mWindowRect.width(), mWindowRect.height());
	dirtyArea = clientRect.Intersect(dirtyArea.Union(gfx::Rect(rect)));
	parent->invalidatePopups();
}

void PopupWidget::didScrollRect(int dx, int dy, const WebKit::WebRect& clip_rect)
{
	WebKit::WebSize size = widget->size();
	dirtyArea = gfx::Rect(size.width, size.height);
	parent->invalidatePopups();
}

void PopupWidget::show(WebKit::WebNavigationPolicy policy)
{
}

void PopupWidget::closeWidgetSoon()
{
	MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(parent, &WebViewProxy::closePopup, this));
}

void PopupWidget::didFocus()
{
	widget->setFocus(true);
}

void PopupWidget::didBlur()
{
	widget->setFocus(false);
}

void PopupWidget::didChangeCursor(const WebKit::WebCursorInfo& cursor)
{
}

WebKit::WebRect PopupWidget::windowRect()
{
	return WebKit::WebRect(nWindowRect);
}

void PopupWidget::setWindowRect(const WebKit::WebRect& rect)
{
	mWindowRect = gfx::Rect(rect);
	widget->resize(WebKit::WebSize(rect.width, rect.height));
	dirtyArea = dirtyArea.union(gfx::Rect(rect.width, rect.height));
}

WebKit::WebRect PopupWidget::rootWindowRect()
{
	return parent->windowRect();
}

WebKit::WebRect PopupWidget::windowResizerRect()
{
	return windowRect();
}

void PopupWidget::runModal() {}

WebKit::WebScreenInfo PopupWidget::screenInfo()
{
	WebKit::WebScreenInfo ret;
	ret.availableRect = WebKit::WebRect(0, 0, 1024, 768);
	ret.depth = 24;
	ret.isMonochrome = false;
	ret.depthPerComponent = 8;
	ret.rect = WebKit::WebRect(0, 0, 1024, 768);
	return ret;
}
